package messages;

/**
 * 
 * This is the Base Message class for all of the messages that get sent out
 * for the Java classes.  Every message is an implementing subclass of this
 * class.
 * 
 * @author Ian Doerr
 */
public abstract class BaseMessage {

	/**
	 * This constructor verifies that data byte array is the minimum length for this
	 * message.  If the length of the message is 0 or less, then this check will do nothing.
	 * 
	 * @param data
	 */
    public BaseMessage(byte[] data)
    {
        if (data != null && data.length < getMessageSize())
        {
            throw new RuntimeException();
        }
    }
    
    /**
     * Abstract byte[] implementation, this is meant to be used for serialization of the messages.
     * 
     * @return
     */
    public abstract byte[] toByteArray();
    
    /**
     * This is the default Byte Array implementation for a message without any parameters
     * 
     * @return
     */
    protected byte[] defaultByteArray()
    {
        byte[] retVal = new byte[getMessageSize()];
        retVal[0] = getMessageType().getIdentifier();
        return retVal;
    }
    
    /**
     * This is meant to return the size of the message, and should be static for messages
     * with static arguments.
     * 
     * @return
     */
    public abstract int getMessageSize();
    
    /**
     * This is meant to represent the Message Type of the current message.
     * 
     * @return
     */
    public abstract MessageType getMessageType();
    
    @Override
    /**
     * Returns a string representation of the current message.
     */
    public String toString()
    {
        return MessageUtils.byteArrayToString(this.toByteArray());
    }

}
